package com.gzdh.dishrecognition.ui.adapter.report;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.ReserveListDto;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class OrderReportAdapter extends RecyclerView.Adapter<OrderReportAdapter.ViewHolder> {


    private Context mcontext;
    private List<ReserveListDto> reserveListDtoList;

    private UserClick userClick;

    public void setUserClick(UserClick userClick) {
        this.userClick = userClick;
    }

    public void setReserveListDtoList(List<ReserveListDto> reserveListDtoList1) {
        this.reserveListDtoList = reserveListDtoList1;
        notifyDataSetChanged();

    }

    public OrderReportAdapter(Context context) {
        mcontext = context;
        this.reserveListDtoList = new ArrayList<>();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unonline, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.nameTv.setText(reserveListDtoList.get(position).getKehu());
        holder.amountTv.setText(new BigDecimal(reserveListDtoList.get(position).getJe()).setScale(2, RoundingMode.DOWN).toPlainString());
        holder.orderTv.setText(reserveListDtoList.get(position).getZfserialnumber());

        holder.createTimeTv.setText(reserveListDtoList.get(position).getCzrq());
        holder.mainLinear.setOnClickListener(view -> {
            if (userClick != null) {
                userClick.onUserClick(reserveListDtoList.get(position).getZfserialnumber());
            }
        });
        if ("0".equals(reserveListDtoList.get(position).getCs())) {
            holder.stateTv.setText("已支付");
        } else {
            holder.stateTv.setText("已退款");
        }
        if ("0".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("自购支付");
        } else if ("1".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("补贴支付");
        } else if ("2".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("现金支付");
        } else if ("3".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("微信支付");
        } else if ("4".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("支付宝支付");
        } else if ("9".equals(reserveListDtoList.get(position).getSc())) {
            holder.scTv.setText("聚合支付");
        }
    }

    @Override
    public int getItemCount() {
        return reserveListDtoList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView orderTv;
        TextView nameTv;
        TextView amountTv;
        TextView createTimeTv;
        TextView stateTv;

        TextView scTv;

        ViewHolder(View item) {
            super(item);
            int size = SysConstConfig.getInstance().getDeviceReportFontSize();
            mainLinear = item.findViewById(R.id.mainLinear);

            orderTv = item.findViewById(R.id.orderTv);
            orderTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

            nameTv = item.findViewById(R.id.nameTv);
            nameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

            amountTv = item.findViewById(R.id.amountTv);
            amountTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

            createTimeTv = item.findViewById(R.id.createTimeTv);
            createTimeTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

            stateTv = item.findViewById(R.id.stateTv);
            stateTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

            scTv = item.findViewById(R.id.scTv);
            scTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    public interface UserClick {
        public void onUserClick(String order);
    }

}
